import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/model/category_page_data.dart';
import 'package:flutter_shop/provide/cart_provide.dart';
import 'package:flutter_shop/provide/goods_detail_provide.dart';
import 'package:flutter_shop/provide/goods_list.dart';
import 'package:flutter_shop/routes/application.dart';
import 'package:provide/provide.dart';

class MainContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        MainContentNavBar()
      ],
    );
  }
}

class MainContentNavBar extends StatefulWidget {
  @override
  _MainContentNavBarState createState() => _MainContentNavBarState();
}

class _MainContentNavBarState extends State<MainContentNavBar> {
  String currentSubItemId = "";

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _navBarList(),
        _goodsList()
      ],
    );
  }

  Widget _goodsList() {
    return Container(
      height: ScreenUtil().setHeight(982.0),
      width: ScreenUtil().setWidth(570.0),
      child: Provide<GoodsProvider>(builder: (context, child, goodsProvider) {
        List list = goodsProvider.goodsList;
        if (list != null && list.length > 0) {
          return ListView.builder(
            itemCount: list.length,
            itemBuilder: (context, index) {
              return GestureDetector(
                child: Container(
                  height: ScreenUtil().setHeight(160),
                  decoration: BoxDecoration(
                    border: index == list.length - 1 ? null : Border(
                      bottom: BorderSide(width: 1.0, style: BorderStyle.solid, color: Colors.black54)
                    )
                  ),
                  child: Row(
                    children: <Widget>[
                      _goodsImage(list[index]),
                      _goodsInfo(list[index])
                    ],
                  )
                ),
                onTap: () {
                  Application.router.navigateTo(context, '/detail?id=${list[index].id}');
                },
              );
            }
          );
        }
        return Center(child: Text('该类目下没有数据'),);
      }),
    );
  }

  Widget _goodsImage(item) {
    return Container(
      padding: EdgeInsets.only(left: 10.0),
      width: ScreenUtil().setWidth(140),
      child: Image.network(item.mainPics[0]),
    );
  }

  Widget _goodsInfo(item) {
    return Container(
      padding: EdgeInsets.all(10.0),
      width: ScreenUtil().setWidth(410),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            item.goodsName,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            textAlign: TextAlign.start,
            style: TextStyle(
              fontWeight: FontWeight.w600,
            ),
          ),
          SizedBox(height: 5.0,),
          Text.rich(
            TextSpan(
              text: '￥${item.price}',
              style: TextStyle(
                color: Colors.pink,
                fontWeight: FontWeight.w600,
                fontSize: 14.0
              ),
              children: [
                TextSpan(
                  text: ' 已售${item.soldNum}',
                  style: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 10.0,
                    color: Colors.black87,
                    decoration: TextDecoration.lineThrough
                  )
                )
              ]
            )
          )
        ],
      ),
    );
  }

  Widget _navBarList() {
    return Container(
      height: ScreenUtil().setHeight(80.0),
      width: ScreenUtil().setWidth(570.0),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 1, color: Colors.black38, style: BorderStyle.solid)
        )
      ),
      child: Provide<CartSubNavList>(builder: (context, child, cartSubNavList) {
        List list = cartSubNavList.navList;
        if (cartSubNavList.navList == null) return Text('');
        return ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: list.length,
          itemBuilder: (context, index) {
            return _navBarItem(cartSubNavList.navList[index]);
          }
        );
      }),
    );
  }

  Widget _navBarItem(CategorySubDataModel item) {
    return Provide<GoodsProvider>(builder: (context, child, goodsProvider) {
      return GestureDetector(
        child: Container(
          padding: EdgeInsets.all(10.0),
          child: Text(
            item.mallSubName,
            style: TextStyle(fontSize: ScreenUtil().setSp(28.0), color: goodsProvider.grade2Id == item.mallSubId ? Colors.pink : Colors.black87),
          ),
        ),
        onTap: () {
          setState(() {
            currentSubItemId = item.mallSubId;

            Provide.value<GoodsProvider>(context).getGoodsList(subId: currentSubItemId);
          });
        },
      );
    });
  }
}